/**
 * @file TcpApp_client_transport.c
 * @brief Transport protocol abstraction layer
 *
 * @section License
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.
 *
 * This file is part of CycloneTCP Open.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * @author Oryx Embedded SARL (www.oryx-embedded.com)
 * @version 2.5.2
 **/

// Switch to the appropriate trace level
#define TRACE_LEVEL TCP_APP_TRACE_LEVEL

// Dependencies
#include "core/net.h"
#include "client/tcp_client_app.h"
#include "client/tcp_client_app_transport.h"
#include "debug.h"

/**
 * @brief Open network connection
 * @param[in] context Pointer to the TcpApp/TCP client context
 * @return Error code
 **/

error_t TcpAppClientOpenConnection(TcpAppClientContext *context)
{
   error_t error;

   // Open a TCP socket
   context->socket = socketOpen(SOCKET_TYPE_STREAM, SOCKET_IP_PROTO_TCP);
   // Failed to open socket?
   if (context->socket == NULL)
      return ERROR_OPEN_FAILED;

   // Associate the socket with the relevant interface
   error = socketBindToInterface(context->socket, context->interface);
   // Any error to report?
   if (error)
      return error;

   // Set timeout
   error = socketSetTimeout(context->socket, context->timeout);
   // Any error to report?
   if (error)
      return error;

   // Successful processing
   return NO_ERROR;
}

/**
 * @brief Establish network connection
 * @param[in] context Pointer to the TcpApp/TCP client context
 * @param[in] serverIpAddr IP address of the TcpApp/TCP server to connect to
 * @param[in] serverPort TCP port number that will be used to establish the
 *   connection
 * @return Error code
 **/

error_t TcpAppClientEstablishConnection(TcpAppClientContext *context,
                                        const IpAddr *serverIpAddr, uint16_t serverPort)
{
   error_t error;

   // Establish TCP connection
   error = socketConnect(context->socket, serverIpAddr, serverPort);
   // Any error to report?
   if (error)
      return error;

   // Successful processing
   return NO_ERROR;
}

/**
 * @brief Shutdown network connection
 * @param[in] context Pointer to the TcpApp/TCP client context
 * @return Error code
 **/

error_t TcpAppClientShutdownConnection(TcpAppClientContext *context)
{
   error_t error;

   // Initialize status code
   error = NO_ERROR;

   // Check status code
   if (!error)
   {
      // Valid TCP socket?
      if (context->socket != NULL)
      {
         // Shutdown TCP connection
         error = socketShutdown(context->socket, SOCKET_SD_BOTH);
      }
   }

   // Return status code
   return error;
}

/**
 * @brief Close network connection
 * @param[in] context Pointer to the TcpApp/TCP client context
 **/

void TcpAppClientCloseConnection(TcpAppClientContext *context)
{
   // Close TCP connection
   if (context->socket != NULL)
   {
      socketClose(context->socket);
      context->socket = NULL;
   }
}

/**
 * @brief Send data using the relevant transport protocol
 * @param[in] context Pointer to the TcpApp/TCP client context
 * @param[in] data Pointer to a buffer containing the data to be transmitted
 * @param[in] length Number of bytes to be transmitted
 * @param[out] written Actual number of bytes written (optional parameter)
 * @param[in] flags Set of flags that influences the behavior of this function
 * @return Error code
 **/

error_t TcpAppClientSendData(TcpAppClientContext *context, const void *data,
                             size_t length, size_t *written, uint_t flags)
{
   error_t error;

   // Transmit data
   error = socketSend(context->socket, data, length, written, flags);

   // Return status code
   return error;
}

/**
 * @brief Receive data using the relevant transport protocol
 * @param[in] context Pointer to the TcpApp/TCP client context
 * @param[out] data Buffer into which received data will be placed
 * @param[in] size Maximum number of bytes that can be received
 * @param[out] received Number of bytes that have been received
 * @param[in] flags Set of flags that influences the behavior of this function
 * @return Error code
 **/

error_t TcpAppClientReceiveData(TcpAppClientContext *context, void *data,
                                size_t size, size_t *received, uint_t flags)
{
   error_t error;

   // Receive data
   error = socketReceive(context->socket, data, size, received, flags);

   // Return status code
   return error;
}
